package com.cx.base.day21_jdbc.jdbc05_千万级数据插入;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.*;
import java.sql.*;
import java.util.Properties;

/**
 * @Author: songyahui
 * @Date: 2020/7/28 21:08
 * @Version 1.0
 */
public class JDBCInsertWithBigNum {
    private static DataSource ds;

    static Connection connection;

    static void propertiesLoading() {
        try {
            //加载配置文件
            Properties pro = new Properties();
            try {
                File f = new File("jdbc.properties");
                InputStream fis = new BufferedInputStream(new FileInputStream(f));
//                InputStream fis = JDBCInsertWithBigNum.class.getClassLoader().getResourceAsStream("jdbc.properties");
                System.out.println("fis:" + fis);
                pro.load(fis);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        return ds;
    }

    public static void exec() {
        try {
//            connection = DriverManager.getConnection("jdbc:mysql://212.64.68.152:3306/test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8","root","root");
            connection = getDataSource().getConnection();
            String sql = "insert into logs1(logtype,logurl,logip) values ('a','b','c')";
            for (int i = 0; i < 10; i++) {

                PreparedStatement statement = connection.prepareStatement(sql);
//            statement.setString();
                //提交sql,并获取返回结果
                int resultSet = statement.executeUpdate();
                System.out.println(resultSet);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        propertiesLoading();
        Thread t1 = new Thread(new ThreadRunable());
        Thread t2 = new Thread(new ThreadRunable());
        Thread t3 = new Thread(new ThreadRunable());
        Thread t4 = new Thread(new ThreadRunable());
        Thread t5 = new Thread(new ThreadRunable());
        long time1 = System.currentTimeMillis();
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        exec();
        long time2 = System.currentTimeMillis();
        System.out.println(time2 - time1);
    }

}

class ThreadRunable implements Runnable {


    @Override
    public void run() {
        JDBCInsertWithBigNum.exec();
    }
}


